package com.pitb.monitorcomplaints;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.text.TextUtils;

public class SmsReciever extends BroadcastReceiver {
	 public static final String NUMBER = "number";
	  public static final String SMS = "sms";
	  
	@Override
	public void onReceive(Context context, Intent intent) {		
		Bundle bundle = intent.getExtras();
		SmsMessage[] msgs = null;
		if (bundle != null) {
			// ---retrieve the SMS message received---
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			for (int i = 0; i < msgs.length; i++) {
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				String number = msgs[i].getOriginatingAddress();
				String text = msgs[i].getMessageBody();
				if(TextUtils.isEmpty(number)){
					return;
				}
				Intent localIntent = new Intent(context,
						SmsPostService.class);
				localIntent.putExtra(NUMBER, number);
				localIntent.putExtra(SMS, text);				
				context.startService(localIntent);
			}
		}

	}

}
